package tsinghua.smxq.ui;

import tsinghua.smxq.mc.R;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ListView;

/*
 * 
 * 本UI组件的实现参考了 eoeandroid论坛acbuwa的代码片段，详见
 * http://www.eoeandroid.com/thread-183314-1-2.html
 * 
 */
public class MCMenuView extends HorizontalScrollView implements
		OnGlobalLayoutListener {

	private ListView menu;

	public static boolean isMenuVisible;

	private final int ENLARGE_WIDTH = 600;

	private int menuWidth;

	private float lastMotionX = -1;

	private Button menuBtn;

	private int current;

	private int scrollToViewPos;

	private ViewGroup parent;

	private View[] children;

	public MCMenuView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
	}

	public MCMenuView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init();
	}

	public MCMenuView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		init();
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		int x = (int) ev.getRawX();

		if (ev.getAction() == MotionEvent.ACTION_DOWN) {
			/* 手指按下时的x坐标 */
			this.lastMotionX = (int) ev.getRawX();
		}
		if ((this.current == 0 && x < this.scrollToViewPos)
				|| (this.current == this.scrollToViewPos * 2 && x > this.ENLARGE_WIDTH)) {
			return false;
		}
		if (isMenuVisible == false && this.lastMotionX > 20) {
			return true;
		}

		else {
			if (ev.getAction() == MotionEvent.ACTION_UP) {
				menuSlide();
				return false;
			}
		}
		return super.onTouchEvent(ev);
	}

	private void menuSlide() {

		if (this.menuWidth == 0) {
			isMenuVisible = true;
		} else {
			isMenuVisible = false;
		}
		this.smoothScrollTo(this.menuWidth, 0);
		if (isMenuVisible == true)
			this.menuBtn.setBackgroundResource(R.drawable.menu_fold);
		else
			this.menuBtn.setBackgroundResource(R.drawable.menu_unfold);
	}

	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		// TODO Auto-generated method stub
		super.onScrollChanged(l, t, oldl, oldt);
		if (l < (this.menu.getMeasuredWidth() - this.menuBtn.getMeasuredWidth() - this.ENLARGE_WIDTH) / 2) {
			this.menuWidth = 0;
		} else {
			this.menuWidth = this.menu.getWidth()
					- this.menuBtn.getMeasuredWidth() - this.ENLARGE_WIDTH;
		}
		this.current = l;
	}

	public void setMenuBtn(Button btn) {
		this.menuBtn = btn;
	}

	public void clickMenuBtn() {

		if (!isMenuVisible) {
			this.menuWidth = 0;
		} else {
			this.menuWidth = this.menu.getMeasuredWidth()
					- this.menuBtn.getMeasuredWidth() - this.ENLARGE_WIDTH;
		}
		menuSlide();
	}

	private void init() {
		this.setHorizontalFadingEdgeEnabled(false);
		this.setVerticalFadingEdgeEnabled(false);
		this.setVisibility(View.INVISIBLE);
		isMenuVisible = false;

	}

	public void initViews(View[] children, Button menuButton, ListView menu) {
		this.menu = menu;
		ViewGroup parent = (ViewGroup) getChildAt(0);

		for (int i = 0; i < children.length; i++) {
			children[i].setVisibility(View.INVISIBLE);
			parent.addView(children[i]);
		}

		this.children = children;
		this.menuBtn = menuButton;
		this.parent = parent;
		getViewTreeObserver().addOnGlobalLayoutListener(this);

	}

	@Override
	public void onGlobalLayout() {
		// TODO Auto-generated method stub
		this.getViewTreeObserver().removeGlobalOnLayoutListener(this);
		this.parent.removeViewsInLayout(0, children.length);
		int width = this.getMeasuredWidth();
		int height = this.getMeasuredHeight();

		int[] dims = new int[2];
		scrollToViewPos = 0;

		for (int i = 0; i < children.length; i++) {
			dims[0] = width;
			dims[1] = height;
			if (i != 1) {
				dims[0] = width - this.menuBtn.getMeasuredWidth()
						- ENLARGE_WIDTH;

			}
			children[i].setVisibility(View.VISIBLE);

			parent.addView(children[i], dims[0], dims[1]);
			if (i == 0) {
				scrollToViewPos += dims[0];
			}
		}
		new Handler().post(new MCRunnable(this));

	}

	class MCRunnable implements Runnable {

		private MCMenuView mv;

		MCRunnable(MCMenuView mv) {
			this.mv = mv;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			this.mv.scrollBy(scrollToViewPos, 0);

			this.mv.setVisibility(View.VISIBLE);
			this.mv.menu.setVisibility(View.VISIBLE);

		}

	}

}
